package com.viewplug.fragment;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.viewplug.R;

/**
 * Created by mac_wk on 14-12-26.
 */
public class TabFragmentActivity extends FragmentActivity {
    protected FragmentTabHost fragmentTabHost;
    protected LayoutInflater layoutInflater;
    protected Class fragmentArray[];
    protected int []icons;
    private String []strs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_plug_tabfragment);
        initviews();
    }
    private void initviews() {

        fragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        fragmentTabHost.setup(this,getSupportFragmentManager(),R.id.realtabcontent);
        layoutInflater = LayoutInflater.from(this);
    }
    protected View addTab(FragmentTabSpec spec)
    {
        View view = getTabItemView(spec);
        TabHost.TabSpec tabSpec = fragmentTabHost.newTabSpec(spec.text).setIndicator(view);
        fragmentTabHost.addTab(tabSpec,spec.fragment,spec.bundle);
        return view;

    }

    private View getTabItemView(FragmentTabSpec spec)
    {
        View view = layoutInflater.inflate(R.layout.view_plug_tab_item,null);
        ImageView imageView = (ImageView) view.findViewById(R.id.viewplug_tab_imageview);
        imageView.setImageResource(spec.icon);
        TextView textView = (TextView) view.findViewById(R.id.viewplug_tab_textview);
        textView.setText(spec.text);

        if (spec.textColor!=-1)
            textView.setTextColor(spec.textColor);
        if (spec.bg_res!=-1)
            view.setBackgroundResource(spec.bg_res);
        return view;
    }
    protected static class FragmentTabSpec
    {
        public int textColor=-1;
        public int bg_res = -1;
        public String text;
        public int icon=-1;
        public Class fragment = null;
        public Bundle bundle;
        public FragmentTabSpec(String text, int icon,Class fragment) {
            this(text,icon,fragment,-1);
        }
        public FragmentTabSpec(String text, int icon,Class fragment,int textColor)
        {

            this(text,icon,fragment,textColor,-1);
        }
        public FragmentTabSpec(String text, int icon,Class fragment,int textColor,int bg_res)
        {
            this.text = text;
            this.icon = icon;
            this.fragment = fragment;
            this.textColor = textColor;
            this.bg_res = bg_res;
        }
        public int getTextColor() {
            return textColor;
        }

        public void setTextColor(int textColor) {
            this.textColor = textColor;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public int getIcon() {
            return icon;
        }

        public void setIcon(int icon) {
            this.icon = icon;
        }

        public Class getFragment() {
            return fragment;
        }

        public void setFragment(Class fragment) {
            this.fragment = fragment;
        }

        public Bundle getBundle() {
            return bundle;
        }

        public void setBundle(Bundle bundle) {
            this.bundle = bundle;
        }
    }
}
